﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark
{
    public partial class ChiTietSP : System.Web.UI.Page
    {
        public SAN_PHAM SanPham;
        public int SoSao;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] != null)
            {
                long IDSP = long.Parse(Request.QueryString["ID"].ToString());
                SanPham = SanPhamModels.LaySanPhamTheoID(IDSP);
                liHinhSP.InnerHtml = GenerateHinh(SanPham);
                rating.InnerHtml = GenerateRation(SanPham.DANH_GIA.ToList());
            }
            else
            {
                Response.Redirect("Error.aspx");
            }
        }

        private string GenerateHinh(SAN_PHAM SanPham)
        {
            StringBuilder str = new StringBuilder(); 
            foreach (var item in SanPham.HINH_SAN_PHAM.Take(3))
	        {
                str.Append("<li class=''> <a href='#a' data-image='images/products/" + item.Hinh + "' data-zoom-image='images/products/" + item.Hinh + "'  class='elevatezoom-gallery' > <img src='images/products/" + item.Hinh + "' alt='small image' /></a></li>");
	        }
            return str.ToString();
        }

        public string GenerateRation(List<DANH_GIA> GG)
        {
            decimal tongsodanhgia = GG.Where(w => w.SoSao != 0).Count();
            decimal tongsosao = GG.Where(w=>w.SoSao!=0).Sum(s => s.SoSao);
            decimal sao = 0;
            if (tongsodanhgia > 0 && tongsosao>0)
                sao = Math.Round((tongsosao / tongsodanhgia));
            return GenerateRatingTheoSoSao(int.Parse(sao.ToString()));
        }

        public string GenerateRatingTheoSoSao(int sao)
        {
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < sao; i++)
            {
                str.Append("<i class='fa fa-star'></i>");
            }
            if (5 - sao > 0)
            {
                for (int i = 0; i < (5 - sao); i++)
                {
                    str.Append("<i class='fa fa-star-o'></i>");
                }
            }
            return str.ToString();
        }

    }
}